﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;
namespace DAO
{
    class LoTrinhTuyen:DataProvider
    {
        protected override object GetDataFromDataRow(DataTable dt, int i)
        {


            LoTrinhTuyenDTO objectDTO = new LoTrinhTuyenDTO();
            objectDTO.MaTuyen = int.Parse(dt.Rows[i]["MaTuyen"].ToString());
            objectDTO.STT = int.Parse(dt.Rows[i]["STT"].ToString());
            objectDTO.MaDoan = int.Parse(dt.Rows[i]["MaDoan"].ToString());
            return (object)objectDTO;
        }

        public LoTrinhTuyenDTO[] GetAll()
        {
            String strSQL = "Select * from LoTrinhTuyen ";
            DataTable dt = new DataProvider().ExecuteQuery(strSQL);

            LoTrinhTuyenDTO[] lotrinhtuyenDTOs = new LoTrinhTuyenDTO[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
                lotrinhtuyenDTOs[i] = (LoTrinhTuyenDTO)GetDataFromDataRow(dt, i);

            return lotrinhtuyenDTOs;
        }

        public void insert(LoTrinhTuyenDTO info)
        {
            string insertCommand = "INSERT LoTrinhTuyen VALUES(" +
                info.MaTuyen + ", '" +
                info.STT + ", '" +
                info.MaDoan + "')";

            ExecuteNonQuery(insertCommand);
        }

        public void update(LoTrinhTuyenDTO info)
        {
            string updateCommand = "UPDATE LoTrinhTuyen " +
                                    "SET MaDoan = '" + info.MaDoan+ "', " +
                                    " WHERE STT ='" + info.STT+"' AND MaTuyen ='"+info.MaTuyen+"'";


            ExecuteNonQuery(updateCommand);
        }

        public void delete(int matuyen,int stt)
        {
            string deleteCommand = "DELETE FROM LoTrinhTuyen WHERE MaTuyen= " + matuyen+" AND STT="+stt;
            ExecuteNonQuery(deleteCommand);
        }
    }
}
